package com.shenmazong.cheat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.cheat.entity.TbExamCheat;
import com.shenmazong.cheat.entity.TbExamCheatType;
import com.shenmazong.cheat.mapper.TbExamCheatTypeMapper;
import com.shenmazong.cheat.service.ExamCheatService;
import com.shenmazong.cheat.mapper.TbExamCheatMapper;
import com.shenmazong.util.ChooseIsNull;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import com.shenmazong.vo.ExamCheatVo;
import com.shenmazong.vo.ListParamVo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *serviceImpl Qi
 */
@Service
public class ExamCheatServiceImpl extends ServiceImpl<TbExamCheatMapper, TbExamCheat>
    implements ExamCheatService {

    @Autowired
    private  TbExamCheatMapper  cheatMapper;
    @Autowired
    private TbExamCheatTypeMapper typeMapper;

    @Override
    public ResultResponse saveExamCheat(TbExamCheat tbExamCheat) {
        int i = cheatMapper.insert(tbExamCheat);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(cheatMapper.selectById(tbExamCheat.getCheatId()));
    }

    @Override
    public ResultResponse deleteExamCheat(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = cheatMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateExamCheat(TbExamCheat tbExamCheat) {
        int i = cheatMapper.updateById(tbExamCheat);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(cheatMapper.selectById(tbExamCheat.getCheatId()));
    }

    @Override
    public ResultResponse listExamCheat(ListParamVo paramVo) {
        //非空判断
        if(paramVo.getParamMap().size()==0 || paramVo.getParamMap().get("schoolId")==null){
            return ResultResponse.FAILED("无效查询条件");
        }

        IPage<ExamCheatVo> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        IPage<ExamCheatVo> selectPage = cheatMapper.selectExamCheatList(page,paramVo.getParamMap());
        return ResultResponse.SUCCESS(selectPage);
    }

    @Override
    public ResultResponse getExamCheat(TbExamCheat tbExamCheat) {
        return ResultResponse.SUCCESS(cheatMapper.selectById(tbExamCheat.getCheatId()));
    }

    @Override
    public ResultResponse saveExamCheatType(TbExamCheatType tbExamCheatType) {
        int i = typeMapper.insert(tbExamCheatType);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(typeMapper.selectById(tbExamCheatType.getCheatTypeId()));
    }

    @Override
    public ResultResponse deleteExamCheatType(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = typeMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateExamCheatType(TbExamCheatType tbExamCheatType) {
        int i = typeMapper.updateById(tbExamCheatType);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(typeMapper.selectById(tbExamCheatType.getCheatTypeId()));
    }

    @Override
    public ResultResponse listExamCheatType(ListParamVo paramVo) {
        Page<TbExamCheatType> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        QueryWrapper<TbExamCheatType> queryWrapper = new QueryWrapper<>();
        if(StringUtils.checkValNotNull(paramVo.getParamMap().get("cheatTypeName"))){
            queryWrapper.lambda().like(TbExamCheatType::getCheatTypeName,paramVo.getParamMap().get("cheatTypeName"));
        }
        Page<TbExamCheatType> selectPage = typeMapper.selectPage(page, queryWrapper);
        return ResultResponse.SUCCESS(selectPage);
    }

    @Override
    public ResultResponse listAllExamCheatType() {
        return ResultResponse.SUCCESS(typeMapper.selectList(null));
    }
}




